<template>
	<div class="app-container">
		<!--  <el-upload style=""
                     class="upload-demo"
                     ref="upload"
                     :limit="fileLimit"
                     drag
                     action="#"
                     :http-request="httpRequest"
                     :on-preview="handlePreview"
                     :on-remove="handleRemove"
                     :on-exceed="handleExceed"
                     :before-remove="beforeRemove"
                     :before-upload="beforeUpload"
                     :on-change="handleChange"
                     :auto-upload="false"
                     :on-success = "onSuccess"
                     accept=".zip"
                     :multiple="true">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">最多{{this.fileLimit}}个附件，每个附件不超过{{this.fileSizeLimitByMb}}MB,当前选择文件数：{{ this.uploadCount }}</div>
            <el-progress style="width: 200px;margin-top: 8px"  :percentage="progressPercent" />
          </el-upload>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="uploadAll">开始上传</el-button>
          </div> -->
	</div>
</template>
<script>
	import {
		Decrypt
	} from "@/utils/keyData";
	import JSZip from "jszip";

	import task from '@/api/task';
	import dept from '@/api/dept';
	import taskdept from '@/api/taskdept';
	import round from '@/api/round';
	import matter from '@/api/matter';
	import fankui from '@/api/fankui';

  import  zipApi  from '@/api/jszipApi';

	export default {
		name: "SelfUploadMultip",
		data() {
			return {
				progressPercent: 0,
				fileLimit: 50,
				fileSizeLimitByMb: 2,
				fileLists: [],
				uploadCount: 0,
				dataJson: {
					jsonStr: []
				},
			};
		},

		methods: {
			onSuccess(response, file, fileList) {
				console.log("1111111111111111111111111111111111")
				this.$refs.upload.clearFiles();
			},
			handleChange(file, fileList) {
				this.fileLists = fileList;
				this.uploadCount = fileList.length;
			},

			sqlByParam(sql) {

				this.$electron.ipcRenderer.send('run-database', sql, []);
				this.$electron.ipcRenderer.on('run-reply', (event, res) => {
					return res;
				});

			},



			uploadAll() {
				if (this.fileLists == 0) {
					this.$message.warning(
						"请选择上传数据，在开始上传"
					);
					return;
				}

				this.$refs.upload.submit();
				//   if(this.fileLists>this.fileLimit){
				//     this.$message.warning(
				//  "当前限制选择"+ this.fileLimit+"个文件，本次选择了 "+fileList.length+"个文件，请重新选择"
				// );
				//   }
				// this.fileLists.forEach((file) => {
				//     console.log(file)
				//    this.httpRequest(file);
				//   });
			},
			httpRequestlao(file) {

			},
			httpRequest(file) {
        // 导入文件 相对路径
        zipApi.endZIP(file);
				console.log(file)
				var new_zip = new JSZip();
				new_zip.loadAsync(file)
					.then(() => {
						new_zip
							.file("data.json")
							.async("string")
							.then((content) => {

								//this.dataJson.jsonStr = JSON.parse(Decrypt(content));
                this.dataJson.jsonStr = JSON.parse(content);
								console.log(this.dataJson.jsonStr)

								//判断轮次本地是否有 如果有删除 没有新增( 现在是直接更新未做判断)
								const roundId = this.dataJson.jsonStr.patrolRound.id;
								console.log(roundId)
							//	dept.postOrPutModel(this.dataJson.jsonStr.dept).then(result => {
                dept.upsertBy('deptId',this.dataJson.jsonStr.dept).then(result => {

									if (result.code === 200) {
										console.log('插入部门成功');
									} else {
										console.log('插入部门失败');
									}

								})
								round.postOrPutModel(this.dataJson.jsonStr.patrolRound).then(result => {
									if (result.code === 200) {
										console.log('插入轮次成功');
									} else {
										console.log('插入轮次失败');
									}

								})

								// 任务列表
								console.log(this.dataJson.jsonStr.patrolTaskList.length)
								if (this.dataJson.jsonStr.patrolTaskList.length > 0) {
									this.dataJson.jsonStr.patrolTaskList.forEach(item => {
										console.log(item)
										task.postOrPutModel(item).then(result => {
											if (result.code === 200) {
												console.log('插入任务成功');
											} else {
												console.log('插入任务失败');
											}

										})
									})
								}

								//任务中间表
								console.log(this.dataJson.jsonStr.patrolTaskDeptList.length)
								if (this.dataJson.jsonStr.patrolTaskDeptList.length > 0) {

									this.dataJson.jsonStr.patrolTaskDeptList.forEach(item => {
										taskdept.postOrPutModel(item).then(result => {
											if (result.code === 200) {
												console.log('插入任务派发中间表成功');
											} else {
												console.log('插入任务派发中间表失败');
											}

										})
									})
								}
								this.$emit('uploadOk',true)
								console.log('fileName',file)
								this.$message.success(`${file.name}导入成功`)
							});
					})
					.catch((err) => {
						this.$emit('uploadOk', false)
						console.log(file)
						// console.log(err.file.name)
						// this.$message.error(
						// 	"此数据包数据规则已不符合导入规则，请重新选择11。"
						// );
						this.$confirm('此数据包数据规则已不符合导入规则请记录文件名：' + file.name, '提示', {
								confirmButtonText: '确定',
								cancelButtonText: '取消',
								showCancelButton: false,
								type: 'warning'
							})
							.then(() => {
								// 用户点击了确定按钮，执行相应的操作
								// 可以在这里编写相关的逻辑代码
							});

					});

				//此数据包数据规则已不符合本次信息采集规则，请点击“首次填报”进行填报。
			},

		},

	}
</script>